package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.MultilevelActivityBuy;
import com.x.erp.rpc.MultilevelActivityBuyRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 多级营销活动购买记录信息
 */
@RestController
@RequestMapping("/multilevelActivityBuy")
public class MultilevelActivityBuyController extends BaseController{

	@Reference
	private MultilevelActivityBuyRpcService multilevelActivityBuyRpcService;// 多级营销活动购买记录信息服务

	/**
	 * 查询多级营销活动购买记录信息列表
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getMultilevelActivityBuyList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivityBuyList(MultilevelActivityBuy multilevelActivityBuy) {
		if(StringUtils.isEmpty(multilevelActivityBuy.getUserId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", multilevelActivityBuyRpcService.selectMultilevelActivityBuyList(multilevelActivityBuy));
	}

	/**
	 * 查询多级营销活动购买记录信息数量
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getMultilevelActivityBuyNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivityBuyNum(MultilevelActivityBuy multilevelActivityBuy) {
		if(StringUtils.isEmpty(multilevelActivityBuy.getUserId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		Map<String, Object> res = new HashMap<>();
		res.put("total", multilevelActivityBuyRpcService.selectMultilevelActivityBuyNum(multilevelActivityBuy));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 根据ID查询多级营销活动购买记录信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getMultilevelActivityBuy", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivityBuy(MultilevelActivityBuy multilevelActivityBuy) {
		if(multilevelActivityBuy == null || StringUtils.isEmpty(multilevelActivityBuy.getBuyId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "购买ID不能为空！");
		}
		//查询app当前版本
		List<MultilevelActivityBuy> multilevelActivityBuys = multilevelActivityBuyRpcService.selectMultilevelActivityBuyList(multilevelActivityBuy);
		if(multilevelActivityBuys != null && multilevelActivityBuys.size() > 0) {
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", multilevelActivityBuys.get(0));
		}
		return new ResultModel(ResultCode.RESULT_FAIL, "查询失败");
	}

	/**
	 * 新增多级营销活动购买记录信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/insertMultilevelActivityBuy", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertMultilevelActivityBuy(MultilevelActivityBuy multilevelActivityBuy) {
		if(multilevelActivityBuy == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "多级营销活动购买记录信息不能为空！");
		}
		if(StringUtils.isEmpty(multilevelActivityBuy.getActivityId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "活动ID不能为空！");
		}
		if(StringUtils.isEmpty(multilevelActivityBuy.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		MultilevelActivityBuy share = new MultilevelActivityBuy();
		share.setUserId(multilevelActivityBuy.getUserId());
		share.setActivityId(multilevelActivityBuy.getActivityId());
		Date cDate = this.getDate(0);
		multilevelActivityBuy.setBuyState(0);
		multilevelActivityBuy.setBuyCreateTime(cDate);
		multilevelActivityBuy.setBuyUpdateTime(cDate);
		multilevelActivityBuy = multilevelActivityBuyRpcService.insertMultilevelActivityBuy(multilevelActivityBuy);
		if(multilevelActivityBuy != null) {
			return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！", multilevelActivityBuy);
		}
		return new ResultModel(ResultCode.RESULT_FAIL, "您已购买过该车！");
	}

	/**
	 * 成交
	 * @param buyId
	 * @return
	 */
	@RequestMapping(value = "/deal", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel deal(String buyId, String shopId, String saleUserId){
		if(StringUtils.isEmpty(buyId)){
			return new ResultModel(ResultCode.RESULT_FAIL, "购买ID不能为空！");
		}
		if(StringUtils.isEmpty(shopId)){
			return new ResultModel(ResultCode.RESULT_FAIL, "车商ID不能为空！");
		}
		if(StringUtils.isEmpty(buyId)){
			return new ResultModel(ResultCode.RESULT_FAIL, "成交员工ID不能为空！");
		}
		MultilevelActivityBuy buy = new MultilevelActivityBuy();
		buy.setBuyId(buyId);
		return multilevelActivityBuyRpcService.updateDeal(buy, shopId, saleUserId);
	}

}
